import vue from 'vue'
import VueRouter from 'vue-router'
import Guide from '@/views/Guide'
import Home from '@/views/Home'
import Create from '@/views/Create'
import ArtDetail from '@/views/ArtDetail'
import User from '@/views/User'
import Onsale from '@/views/User/OnSale'
import NotOnsale from '@/views/User/NotOnsale'
import Created from '@/views/User/Created'
import MyCollections from '@/views/User/MyCollections'
import LikeArt from '@/views/User/Like/LikeArt'
import LikeCollection from '@/views/User/Like/LikeCollection'
import Followed from '@/views/User/Followed'
import Collections from '@/views/Collections'
import CollectionDetail from '@/views/CollectionDetail'
import UserEdit from '@/views/UserEdit'
import Search from '@/views/Search'
import SearchArt from '@/views/Search/SearchArt'
import SearchCollection from '@/views/Search/SearchCollection'
import SearchUser from '@/views/Search/SearchUser'
import CollectionEdit from '@/views/CollectionEdit'

// api
import {
    decodeUserAddress
} from '@/api'

// 使用插件
vue.use(VueRouter)

const router = new VueRouter({
    routes: [{
            path: '/guide',
            component: Guide
        },
        {
            path: "/hot",
            component: () => import('@/views/Hot'),
            children: [{
                    path: 'nfts',
                    component: () => import('@/views/Hot/HotNfts')
                },
                {
                    path: 'collections',
                    component: () => import('@/views/Hot/HotCollections')
                }
            ],
            redirect: '/hot/nfts'
        },
        {
            path: '/artwork',
            component: Home
        },
        {
            path: '/collections',
            component: Collections
        },
        {
            path: '/create',
            component: Create
        },
        {
            path: '/art/:id',
            component: ArtDetail,
        },
        {
            path: '/edit/user/:address',
            component: UserEdit
        },
        {
            path: '/user/:address',
            component: User,
            children: [{
                    path: 'onsale',
                    component: Onsale
                },
                {
                    path: 'mybid',
                    component: () => import("@/views/User/MyBid")
                },
                {
                    path: 'notonforsale',
                    component: NotOnsale,
                    children: [{
                            path: "success",
                            component: () => import('@/views/User/NotOnsale/Success')
                        },
                        {
                            path: "refushed",
                            component: () => import("@/views/User/NotOnsale/Refushed")
                        },
                        {
                            path: "audit",
                            component: () => import("@/views/User/NotOnsale/Audit")
                        },
                        {
                            path: 'mysole',
                            component: () => import("@/views/User/NotOnsale/MySole")
                        }
                    ],
                    redirect: 'notonforsale/success'
                },
                {
                    path: 'created',
                    component: Created
                }, {
                    path: 'collections',
                    component: MyCollections
                },
                {
                    path: 'like/art',
                    component: LikeArt,
                },
                {
                    path: 'like/collection',
                    component: LikeCollection
                },
                {
                    path: 'followed',
                    component: Followed
                },

            ],
            redirect: '/user/:address/onsale'
        },
        {
            path: '/artist/:address',
            component: () => import("@/views/Artist"),
            children: [{
                    path: 'onsale',
                    component: () => import("@/views/Artist/ArtistOnSale")
                },
                {
                    path: 'collections',
                    component: () => import("@/views/Artist/ArtistCollections")
                },
                {
                    path: 'created',
                    component: () => import("@/views/Artist/ArtistCreated")
                },
                {
                    path: 'like/art',
                    component: () => import("@/views/Artist/ArtistLike/ArtistLikeArt")
                },
                {
                    path: 'like/collection',
                    component: () => import("@/views/Artist/ArtistLike/ArtistLikeCollection")
                }
            ],
            redirect: '/artist/:address/onsale'
        },
        {
            path: '/collection/:id',
            component: CollectionDetail
        },
        {
            path: '/search',
            component: Search,
            children: [{
                    path: 'arts',
                    component: SearchArt
                },
                {
                    path: 'collections',
                    component: SearchCollection
                },
                {
                    path: 'users',
                    component: SearchUser
                },

            ],
            redirect: '/search/collections'
        },
        {
            path: '/searchpage',
            component: () => import("@/views/SearchPage")
        },
        {
            path: '/edit/collection/:id',
            component: CollectionEdit
        },
        {
            path: '/',
            redirect: '/artwork'
        },
        {
            path: '/login',
            component: () => import("@/views/LoginPage")
        }
    ],
    scrollBehavior(to, from, savedPosition) {
        return {
            x: 0,
            y: 0,
            behavior: 'smooth'
        }

    },


})

router.beforeEach(checkToken)


function checkToken(to, from, next) {
    if (to.path == '/login') {
        next()
    } else {
        // 获取token
        let token = localStorage.getItem('token')
        if (token == null) {
            // 验证token是否过期
            // linkToLogin("未登录")
            next('/login')
        } else {
            decodeUserAddress(token).then(result => {
                if (result.flag == false) {
                    localStorage.removeItem('token')
                    linkToLogin(result.msg)
                    return false
                } else {
                    next()
                }
            }).catch(err => {
                console.log(err)
            })
        }
    }

}

export default router